
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/


#pragma once

#include "IPrimitive.h"
#include "CRay.h"
#include <vector>
#include <map>
#include <string>
using namespace std;

class IPrimitive;
class ILight;
class ITexture;

class IProcTexture;
class CTexture;

class CSimpleScene
{
private:
	//singleton variables
	static CSimpleScene* m_instance;

	//storage for primitives
	vector<IPrimitive*> m_primitives;

	//storage for lights
	vector<ILight*> m_lights;

	//storage for materials
	map<string, CMaterial*> m_materials;

	//storage for texture
	vector<IProcTexture*> m_textures;

	//storage for file texture
	vector<CTexture*> m_fileTexture;

	//constructor
	CSimpleScene();

	//destructor
	~CSimpleScene();

	//scene global ambient light
	CColor m_globalAmbientLight;

public:
	//singleton gettor and closer
	static CSimpleScene& getInstance();
	void close();

	IPrimitive* getPrimitive(int index)const;
	CMaterial* getMaterial(const string& materialName)const;

	//Return the light with the index id. Return 0 if the index is out of range.
	ILight* getLight(unsigned int id)const;

	IPrimitive* removePrimitive(int index);

	//Test collision between a ray and the scene.
	//Returns the id and the distance between the ray origin and the first primitive.
	//If the ray doesn't intersect with any primitive, i return -1.
	int getFirstCollision(const CRay& ray, float& dist);

	bool addPrimitive(IPrimitive* primitive);
	bool addLight(ILight* light);
	bool addMaterial(CMaterial* material, const string& matName);
	bool addProcTexture(IProcTexture* texture);

	//store a texture coming from a file
	bool addFileTexture(CTexture* texture);

	//const CColor computeLightColor(const CVector3f& view, const CPoint3f& center, const CNormal3f& normal, 
	//	const IPrimitive& p);

	//Test if the ray collide with a primitive in the scene. 
	//dist is the length of the ray. A negative number means an infinte length.
	//return true if it collide or false if no collision is detected.
	bool testCollision(const CRay& ray, float dist);

	void applyViewMatrix(const CMatrix& view);

	//Return the number of source lights in the scene
	unsigned int getNumberOfLights()const;

	//set the scene global ambient light
	void setGlobalAmbientLight(const CColor& gal);

	//get the scene global ambient light
	CColor getGlobalAmbientLight()const;

};

#define RTSCENE CSimpleScene::getInstance()

